define(['amazeui', 'jquery', 'global/url', 'global/constants'],
  function(amazeui, jquery, url, constants) {
    return ['$scope', '$rootScope', 'UrlService', 'StoreService', 'AdminService', 'UtilService',
    		 function($scope, $rootScope, UrlService, StoreService, AdminService, UtilService) {
        $scope.awesomeThings = [
	      'HTML5 Boilerplate',
	      'AngularJS',
	      'Karma'
        ];
	    
	    var accessToken = $scope.accessToken, filter = {
        	'pagesize': 10,
        	'pno': 1,
        	'search': ''
        }, params = {
        	'accessToken': accessToken,
        	'filter': filter
        };

        $scope.searchstores = function (event) {
        	params.filter.search = $scope.search;
        	params.filter.pagesize = $scope.pagesize;
        	_getStoreItemList(params);
        } 

        $scope.changestorePages = function (event) {
        	params.filter.search = $scope.search;
        	params.filter.pagesize = $scope.pagesize;
        	_getStoreItemList(params);
        }

        $scope.forwardPage = function (event) {
        	var pno = event.target.text;
        	params.filter.search = $scope.search;
        	params.filter.pagesize = $scope.pagesize;
        	params.filter.pno = pno;
        	_getStoreItemList(params);
        }

        $scope.prePage = function () {
        	var currentPage = $scope.stores.currentPage;
        	if (currentPage === 1) {
        		return;
        	}
        	params.filter.search = $scope.search;
        	params.filter.pagesize = $scope.pagesize;
        	params.filter.pno = currentPage - 1;
        	_getStoreItemList(params);
        }

        $scope.nextPage = function () {
        	var currentPage = $scope.stores.currentPage;
        	if (currentPage === $scope.stores.totalPagers) {
        		return;
        	}
        	params.filter.search = $scope.search;
        	params.filter.pagesize = $scope.pagesize;
        	params.filter.pno = currentPage + 1;
        	_getStoreItemList(params);
        }


	    $scope.selectSingleRecord = function (event) {
        	var $ele = event.target;
        	if ($ele.checked) {
        		$('.select-single-record').prop('checked', false);
        		$ele.checked = true;
        	}

        };
        $scope.remove = function(store){
			var storeId = store.id;
			$('#delete-store-modal .delete-store-btn').data('id', storeId);
			$('#delete-store-modal').modal();
        };

        $scope.edit = function(store) {
			$scope.id  = store.id;
			$scope.storeItemName  = store.itemName;
			$scope.storeSku  = store.sku;
			$scope.storePrice  = store.price;
			$scope.storeQuantity  = store.quantity;
			$scope.storeDescription  = store.description;
		};


	    function _getStoreItemList (params) {
	    	StoreService.listStores(params).success(function(response, status) {
	  		    if (response.jsonResponse.statusCode == constants.responseCode.SUCCESS) {
	  		       $scope.stores = response.jsonResponse.response.data;
	  	        } else if (response.statusCode == constants.responseCode.LOGIN_ANOTHER_DEVICE) {
	  	          // childService.Child.logout();
	  	          //window.location.href = constants.pages.guide;
	  	        }
            });
	    }
	    UtilService.renderSelect();
	   _getStoreItemList(params);
    }];
  }
)
